<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="visible"
    width="800px"
    :before-close="handleClose"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      size="medium"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="一级视角" prop="firstView">
            <el-input
              v-model="formData.firstView"
              placeholder="请输入一级视角"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="二级视角" prop="secondView">
            <el-input
              v-model="formData.secondView"
              placeholder="请输入二级视角"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="指标名称" prop="indexName">
            <el-input
              v-model="formData.indexName"
              placeholder="请输入指标名称"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="指标单位" prop="unit">
            <el-input
              v-model="formData.unit"
              placeholder="请输入指标单位"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="指标类型" prop="type">
            <el-select v-model="formData.type" placeholder="请选择指标类型" clearable style="width: 100%;">
            <el-option label="全市通用" value="1" />
            <el-option label="区县特色" value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="年份" prop="year">
                <el-select v-model="formData.year" placeholder="请选择年份" clearable style="width: 100%;">
                    <el-option v-for="year in yearOptions" :key="year" :label="year" :value="year" />
                </el-select>
            </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="指标值" prop="indexValue">
            <el-input
              v-model="formData.indexValue"
              placeholder="请输入指标值"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分区字段" prop="departField">
            <el-input
              v-model="formData.departField"
              placeholder="请输入分区字段"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="位置" prop="location">
            <el-select v-model="formData.location" placeholder="请选择位置" clearable style="width: 100%;">
              <el-option label="左侧" value="左侧" />
              <el-option label="右侧" value="右侧" />
              </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="字段顺序" prop="fieldJson">
        <el-input
          v-model="formData.fieldJson"
          type="textarea"
          :rows="3"
          placeholder="请输入字段顺序"
        />
      </el-form-item>

      <el-form-item label="评价结果" prop="judgeResult">
        <el-input
          v-model="formData.judgeResult"
          type="textarea"
          :rows="3"
          placeholder="请输入评价结果"
        />
      </el-form-item>

      <el-form-item label="指标定义" prop="indexDefine">
        <el-input
          v-model="formData.indexDefine"
          type="textarea"
          :rows="4"
          placeholder="请输入指标定义"
        />
      </el-form-item>

      <el-form-item label="指标结论" prop="indexResult">
        <el-input
          v-model="formData.indexResult"
          type="textarea"
          :rows="3"
          placeholder="请输入指标结论"
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">
        {{ isEdit ? '更新' : '创建' }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, watch, nextTick } from 'vue'
import { Message } from 'element-ui'
import type { ElForm } from 'element-ui/types/form'

interface IndicatorFormData {
  firstView: string
  secondView: string
  indexName: string
  unit: string
  indexValue: string
  departField: string
  fieldJson: string
  judgeResult: string
  indexDefine: string
  indexResult: string
  type: string
  year: string
  location: string
}

interface Props {
  visible: boolean
  isEdit: boolean
  editData?: Partial<IndicatorFormData>
  yearOptions: string[]
}

interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'submit', data: IndicatorFormData): void
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  isEdit: false,
  editData: () => ({}),
  yearOptions: () => [] as string[]
})

const emit = defineEmits<Emits>()

const formRef = ref<ElForm>()
const submitLoading = ref(false)

const formData = reactive<IndicatorFormData>({
  firstView: '',
  secondView: '',
  indexName: '',
  unit: '',
  indexValue: '',
  departField: '',
  fieldJson: '',
  judgeResult: '',
  indexDefine: '',
  indexResult: '',
  type: '',
  year: '',
  location: ''
})

const formRules = {
  firstView: [
    { required: true, message: '请输入一级视角', trigger: 'blur' }
  ],
  secondView: [
    { required: true, message: '请输入二级视角', trigger: 'blur' }
  ],
  indexName: [
    { required: true, message: '请输入指标名称', trigger: 'blur' }
  ],
  unit: [
    { required: true, message: '请输入指标单位', trigger: 'blur' }
  ],
  // indexValue: [
  //   { required: true, message: '请输入指标值', trigger: 'blur' }
  // ],
  type: [
    { required: true, message: '请选择指标类型', trigger: 'blur' }
  ],
  year: [
    { required: true, message: '请选择年份', trigger: 'blur' }
  ],
  location: [
    { required: true, message: '请选择位置', trigger: 'blur' }
  ]
}

const dialogTitle = computed(() => {
  return props.isEdit ? '编辑指标' : '新建指标'
})

const visible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

watch(
  () => props.editData,
  (newData) => {
    if (newData && props.isEdit) {
      Object.assign(formData, {
        firstView: newData.firstView || '',
        secondView: newData.secondView || '',
        indexName: newData.indexName || '',
        unit: newData.unit || '',
        indexValue: newData.indexValue || '',
        departField: newData.departField || '',
        fieldJson: newData.fieldJson || '',
        judgeResult: newData.judgeResult || '',
        indexDefine: newData.indexDefine || '',
        indexResult: newData.indexResult || '',
        type: newData.type || '',
        year: newData.year || '',
        location: newData.location || ''
      })
    }
  },
  { immediate: true, deep: true }
)

watch(
  () => props.visible,
  (newVisible) => {
    if (newVisible && !props.isEdit) {
      resetForm()
    }
    if (newVisible) {
      nextTick(() => {
        formRef.value?.clearValidate()
      })
    }
  }
)

const resetForm = () => {
  Object.assign(formData, {
    firstView: '',
    secondView: '',
    indexName: '',
    unit: '',
    indexValue: '',
    departField: '',
    fieldJson: '',
    judgeResult: '',
    indexDefine: '',
    indexResult: '',
    type: '',
    year: '',
    location: ''
  })
}

const handleClose = () => {
  emit('update:visible', false)
}

const handleSubmit = async () => {
  try {
    const valid = await formRef.value?.validate()
    if (!valid) return

    submitLoading.value = true
    emit('submit', { ...formData })
  } catch (error) {
    console.error('Form validation failed:', error)
  } finally {
    submitLoading.value = false
  }
}
</script>

<style scoped lang="scss">
.dialog-footer {
  text-align: right;
  
  .el-button {
    margin-left: 10px;
  }
}

:deep(.el-dialog__body) {
  padding: 20px 25px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
}

:deep(.el-textarea__inner) {
  resize: vertical;
}
</style> 